const glob = require("glob");
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpack = require("webpack")
const TerserPlugin = require('terser-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const CopyPlugin = require('./tools/copy-plugin');


var dev=process.env.NODE_ENV==='dev';
const port = 20001;

function entry(name){
    if(dev){
        return [name];
    }
    return name;
}

function entries(files){
    var ret={};
    files.map((val) => {
        var name = '';
        if (val.indexOf('entry') > -1) {
            name=val.slice((`./static/script/`).length, 0 - '.entry.js'.length);
        } else {
            name=val.slice((`./static/script/`).length);
        }
        ret[name]=entry(val)
    });
    return ret;
}

var files = glob.sync(`./static/script/**/*.entry.js`);

let webpackConfig = {
   	devtool: dev?"eval":false,
   	mode: dev ? "development":"production",
    resolve:{
        extensions:[ '.js', '.jsx'],
    },
  	entry: entries(files),
  	output: {
    	path: __dirname + (`/dist/static/script`),
    	filename: '[name].js',
    	publicPath: `/static/script/`
  	},
  	module:{
  		rules: [
  			{
  				test: /\.jsx?$/,
		        loaders: 'babel-loader',
		        options:{
		            babelrc:false,
		            presets: ['@babel/env'],
		            plugins: [
		              	["@babel/plugin-syntax-dynamic-import"],
                        ["@babel/plugin-transform-runtime"],
                        ["@babel/plugin-proposal-class-properties", { "loose": true }],
                        ["module-resolver" , {
                            "alias": {
                                "@": "./static/script"
                            }
                        }],
		            ],
		            sourceType:"unambiguous",
          		},
  			},
  			// { test: /\.css$/, loader: "style!css" },
  		]
  	},
  	plugins: [
        new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
        new CompressionPlugin({algorithm: 'gzip'}),
        new CopyPlugin(),
    ],
    optimization: {
      	minimizer: [],
	    minimize: !dev,
	    namedChunks: true,
	    splitChunks:{
	        chunks: "all",
	        minSize: 2000,
	        minChunks: 1,
	        maxAsyncRequests: 5,
	        maxInitialRequests: 3,
	        name: "common",
	    },
    },
    devServer:{
    	contentBase: path.join(__dirname, 'dist'),
	    compress: true,
	    port: port,
        transportMode: 'ws',
    	proxy: [
      	    {
        		context: ["/"],
        		target: "http://localhost:" + (port-1),
      	    },{
                context: ["/duhuijie"],
                target: 'http://localhost:'+ (port-1) ,
                ws: true,
                // secure: false,
            }
    	],
    	hot: true,
        hotOnly:false,
        liveReload:false
    }
 
};
if(dev){
  	webpackConfig.plugins.push( new webpack.NamedModulesPlugin(),new webpack.HotModuleReplacementPlugin())
}else {
  	webpackConfig.optimization.minimizer.push(
	    new TerserPlugin({ 
	       terserOptions: {
	          warnings: false,
	          parse: {},
	          compress: {
	            drop_debugger: true,
	            drop_console: true
	          },
	          output: {
	              beautify: false,
	          },
	        }
	    })
  	)
}


module.exports = webpackConfig
